package com.cbgolf.oa.service;

/**
 * Created by lq on 2018/5/14.
 */

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;

/**
 * @author lq
 */
public class LocationServiceNew extends Service {

  public final String TAG = "LocationService";
  private AMapLocationClient locationClient = null;
  private AMapLocationClientOption locationOption = new AMapLocationClientOption();
  private LatLng last_latlng;

  @Override
  public IBinder onBind(Intent intent) {

    return null;
  }

  @Override
  public void onCreate() {
    Notification noti = new Notification();
    noti.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    startForeground(1, noti);


  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    locationClient.startLocation();
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    if (locationClient != null) {
      locationClient.stopLocation();
      destroyLocation();
    }
    super.onDestroy();
  }

  private void initLocation() {
    if (locationClient == null) {
      //初始化client
      locationClient = new AMapLocationClient(this.getApplicationContext());
    }

    //初始化定位参数
    initLocationOption();

    //设置定位参数

    locationClient.setLocationOption(locationOption);
    // 设置定位监听
    locationClient.setLocationListener(locationListener);
  }

  private void initLocationOption() {

    if (null == locationOption) {
      locationOption = new AMapLocationClientOption();
    }
    //定位精度:高精度模式
    locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
    //设置定位缓存策略
    locationOption.setLocationCacheEnable(false);
    //gps定位优先
    locationOption.setGpsFirst(false);
    //设置定位间隔
//        locationOption.setInterval(3000);
    locationOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是ture
    locationOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
    locationOption.setOnceLocationLatest(true);//true表示获取最近3s内精度最高的一次定位结果；false表示使用默认的连续定位策略。
    locationOption
        .setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
    //AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP

  }

  /**
   * 定位监听
   */
  AMapLocationListener locationListener = new AMapLocationListener() {
    @Override
    public void onLocationChanged(AMapLocation loc) {
      if (null != loc) {//定位成功

//              Toast.makeText(getApplicationContext(), loc.getLatitude()+"---"+loc.getLongitude(),0).show();
        Log.i(TAG, "定位成功");
        Log.i(TAG, "定位的经度位置是：" + loc.getLatitude());
        Log.i(TAG, "定位的维度位置是：" + loc.getLongitude());

      } else {//定位失败
        Log.i(TAG, "定位失败");
      }

      locationClient.stopLocation();
      destroyLocation();


    }
  };

  private void destroyLocation() {
    if (null != locationClient) {
      /**
       * 如果AMapLocationClient是在当前Activity实例化的，
       * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
       */
      locationClient.onDestroy();
      locationClient = null;
      locationOption = null;
    }
  }

  /**
   * 判断两个点之间的距离大于规定的距离
   */
  private boolean IsDistanceMoreOneMile(LatLng last, LatLng news) {
    if (last == null) {
      Log.i(TAG, "第一次为空");
      return true;
    }
    float mi = AMapUtils.calculateLineDistance(last, news);
    Log.i(TAG, "两次的间隔为：" + mi);
    if (mi > 2.0f) {
      return true;
    } else {
      return false;
    }
  }
}

//        boolean Flag= ServiceUtil.isServiceRunning(this,"zph.zhjx.com.chat.service.LocationService");
//
//        服务的开启和关闭
//        Intent intet1 = new Intent(SettingActivity.this, LocationService.class);
////开启
//    startService(intet1);
////关闭
//    stopService(intet1);